var roleBuilder = {

    /** @param {Creep} creep **/
    run: function (creep) {
        //creep.say(creep.memory.building);  //说话测试
        if (creep.store[RESOURCE_ENERGY] == 0) {
            creep.memory.building = true;
        }
        else {
            creep.memory.building = false;
        }

        if (creep.memory.building == true) {
            //creep.say(creep.memory.building);    //说话测试
            var targetsgo = creep.room.find(FIND_STRUCTURES, {
                filter: (structure) => {
                    return structure.structureType == STRUCTURE_STORAGE && structure.store.getUsedCapacity() > 0;
                }
            });
            if (targetsgo.length > 0) {
                if (creep.withdraw(targetsgo[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targetsgo[0]);
                    //creep.say('建来');    //说话测试
                }
            }
        }
        else {
            var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
            if (targets.length > 0) {
                if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0]);
                    //creep.say('建去');    //说话测试
                }
            }
            else {
                var targetsfix = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => structure.hits < structure.hitsMax &&
                        (structure.structureType == STRUCTURE_WALL)//只修理墙
                });
                targetsfix.sort((a, b) => a.hits - b.hits);
                if (targetsfix.length > 0) {
                    if (creep.repair(targetsfix[0]) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targetsfix[0], { visualizePathStyle: { stroke: '#ffaa00' } });
                        //creep.say('修去');
                    }
                }
            }
        }
    }
};

module.exports = roleBuilder;